package com.songcha.sakura.mvvm.comic.search

import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.sakura.bean.ComicListBean
import com.songcha.sakura.config.BaseConfig
import com.songcha.sakura.util.UrlUtil
import io.reactivex.Observable
import org.jsoup.Jsoup

class ComicSearchRepository : BaseRepository()  {

    fun getComicList(key:String,currentPage: Int):Observable<ComicListBean>{
        return createNetworkObservable {
            val data= mutableListOf<ComicListBean.ComicBean>()

            val document = Jsoup.connect("${BaseConfig.BASE_URL}/search.asp?page=$currentPage&searchword=${UrlUtil.urlEncoded(key)}").get()

            val clearfix=document.getElementsByClass("myui-vodlist__media clearfix")

            val lis=clearfix[0].getElementsByTag("li")
            for(li in lis){
                val a=li.child(0).child(0)
                val title=a.attr("title")
                val href=a.attr("href")
                val data_original=a.attr("data-original")

                val update=a.select("span[class=pic-text text-right]").text()
                val tag_top=a.select("span[class=pic-tag pic-tag-top]")[0]
                val score=tag_top.child(0).text()
                val year=tag_top.child(1).text()
                val d=li.child(1)
                val describe=d.select("p").text()

                val b= ComicListBean.ComicBean(title,href,data_original,update,score,year,describe)
                data.add(b)
            }

            val bean=ComicListBean()
            bean.data=data
            it.onNext(bean)
            it.onComplete()

        }

    }

   /* override suspend fun sendRequest_(json: JSONObject?): BaseBean {

        val data= mutableListOf<ComicListBean.ComicBean>()

        var key = ""
        var page=1
        if (json != null) {
            if (json.has("key"))
                key = json.getString("key")
            if (json.has("page"))
                page = json.getInt("page")

        }

        val document = Jsoup.connect("${RetrofitClient.BASE_URL}/search.asp?page=$page&searchword=${UrlUtil.urlEncoded(key)}").get()


        val lis=document.getElementsByClass("myui-vodlist__media clearfix")[0].getElementsByTag("li")
        for(li in lis){
            val a=li.child(0).child(0)
            val title=a.attr("title")
            val href=a.attr("href")
            val data_original=a.attr("data-original")

            val update=a.select("span[class=pic-text text-right]").text()
            val tag_top=a.select("span[class=pic-tag pic-tag-top]")[0]
            val score=tag_top.child(0).text()
            val year=tag_top.child(1).text()
            val d=li.child(1)
            val describe=d.select("p").text()

            val b= ComicListBean.ComicBean(title,href,data_original,update,score,year,describe)
            data.add(b)
        }

        val bean= ComicListBean(data)

        return bean

        //return RetrofitClient.api.getHomeCartList().await()
    }*/



}